package com.xin.springcloudconsumer.service.impl;

import com.sun.org.apache.xml.internal.security.keys.KeyUtils;
import com.xin.springcloudconsumer.dto.UserDTO;
import com.xin.springcloudconsumer.feigns.UserFeign;
import com.xin.springcloudconsumer.form.UserForm;
import com.xin.springcloudconsumer.service.UserService;
import com.xin.springcloudconsumer.utils.KeyUtil;
import com.xin.springcloudconsumer.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserFeign userFeign;

    /**
     * 创建用户
     * @param userForm
     * @return
     */
    public UserDTO create(UserForm userForm) {
        userForm.setNickName("user_"+ KeyUtil.genUniqueKey());
        ResultVO resultVO = userFeign.create(userForm);
        if (resultVO.getCode()==0){
            UserDTO userDTO= (UserDTO) resultVO.getData();
              return userDTO;
        }
        return null;
    }

    /**
     * 更新用户
     * @param userForm
     * @return
     */
    public UserDTO update(UserForm userForm) {
        ResultVO resultVO = userFeign.update(userForm);
        if (resultVO.getCode()==0){
            UserDTO userDTO= (UserDTO) resultVO.getData();
            return userDTO;
        }
        return null;
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    public boolean delete(String id) {
        ResultVO resultVO = userFeign.delete(id);
        if (resultVO.getCode()==0){
            return true;
        }
        return false;
    }

    /**
     * 根据用户ID查询用户
     * @param uid
     * @return
     */
    public UserDTO findOneByUserId(String uid) {
        ResultVO resultVO = userFeign.findOneByUserId(uid);
        if (resultVO.getCode()==0){
              UserDTO userDTO= (UserDTO) resultVO.getData();
              return userDTO;
        }
        return null;
    }

    /**
     * 根据用户openId查询用户
     * @param openId
     * @return
     */
    public UserDTO findOneByOpenId(String openId) {
        ResultVO resultVO = userFeign.findOneByOpenId(openId);
        if (resultVO.getCode()==0){
              UserDTO userDTO= (UserDTO) resultVO.getData();
              return userDTO;
        }
        return null;
    }
}
